//使用jq方式
//var http="http://47.244.16.146:8090";
var http = "http://127.0.0.1:8090";
$(function () {
    // 登录
    $("#loginButton").click(function () {
        console.log("登录按钮被点击");
        login();
    });
    // 注册
    $("#register").click(function () {
        console.log("注册按钮被点击");
        register();
    });
    // 注册页面的登录按钮
    $("#login").click(function () {
        console.log("登录按钮被点击");
        window.location.href = "./login.html"
    });

    // 登录页面的注册按钮
    $("#login_register").click(function () {
        console.log("注册按钮被点击");
        window.location.href = "./register.html"
    });
});

function isEmpty(obj) {
    return typeof obj == "undefined" || obj == null || obj === "";
}

// 注册JS
function register() {
    var userName = $("input[name='userName']").attr("value");
    var passWord = $("input[name='passWord']").attr("value");
    var mobile = $("input[name='mobile']").attr("value");
    // jQuery中获得选中select值
    var sex = $("option[name='sex']:selected").val();
    var hobby = $("input[name='hobby']:checked").val();
    var location = $("option[name='location']:selected").val();
    var describe = $("input[name='describe']").attr("value");
    if (isEmpty(userName) || isEmpty(passWord) || isEmpty(mobile) || isEmpty(sex) || isEmpty(hobby) || isEmpty(location) || isEmpty(describe)) {
        alert("请填写相关信息")
    } else {
        var address = http + "/register/addUser";
        var jsonData = $("#registerInfoForm").serializeObject();
        //alert(JSON.stringify(jsonData));
        $.ajax({
            type: "post",
            dateType: "json",
            url: address,
            data: jsonData,
            success: function (response) {
                console.log(response);
                // 转换成JavaScript对象
                var resultJson = JSON.parse(response);
                //var size = Object.keys(resultJson);
                var a = resultJson.hasOwnProperty("result");
                console.log(a);
                // 判断长度
                if (resultJson.hasOwnProperty("result")) {
                    if (!resultJson.result) {
                        if (resultJson.hasOwnProperty("passWord")) {
                            $("#passWordError").text(resultJson.passWord);
                        } else {
                            $("#passWordError").text("");
                        }
                        if (resultJson.hasOwnProperty("mobile")) {
                            $("#mobileError").text(resultJson.mobile);
                        } else {
                            $("#mobileError").text("");
                        }
                        if (resultJson.hasOwnProperty("userName")) {
                            $("#userNameError").text(resultJson.userName);
                        } else {
                            $("#userNameError").text("");
                        }
                        if (resultJson.hasOwnProperty("msg")) {
                            alert(resultJson.msg);
                        }
                    } else {
                        alert("注册成功");
                        window.location.href = "login.html";
                    }
                }
            },
            error: function () {
                //window.location.href = "index.html";
                alert("系统错误");
            }
        });
    }
}

// 登录JS
function login() {
    //通过jquery获取输入框的值
    var mobile = $("input[name='mobile']").attr("value");
    var passWord = $("input[name='passWord']").attr("value");
    if (!isEmpty(mobile) && !isEmpty(passWord)) {
        console.log("长度合格");
        var address = http + "/user/login";
        $.ajax({
            type: "post",
            dateType: "json",
            url: address,
            data: $('#login').serialize(),
            success: function (result) {
                console.log(result);
                alert(result['msg']);
            },
            error: function () {
                alert("系统错误");
                window.location.href = "login";
            }
        });
    } else {
        alert("用户名或者密码格式错误");
        //return false;
    }
}